home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / mail / emailobj.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  216 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from util import autoassign, strip_html_and_tags, replace_newlines, Storage, fuzzydecode
  5. from email.utils import parseaddr, parsedate
  6. from email.header import decode_header
  7. from datetime import datetime
  8. import email.message as email
  9. from common import pref
  10. import sys
  11. import traceback
  12. import logging
  13. log = logging.getLogger('emailobj')
  14. UnicodeErrors = (UnicodeEncodeError, UnicodeDecodeError)
  15.  
  16. def unicode_hdr(hdr, fallback = None):
  17.     more_encs = None if fallback else []
  18.     
  19.     try:
  20.         return (u''.join,)((lambda .0: for hdr, encoding in .0:
  21. None if encoding else hdr)(decode_header(hdr)))
  22.     except UnicodeErrors:
  23.         return fuzzydecode(hdr, more_encs + [
  24.             'utf-8'])
  25.  
  26.  
  27.  
  28. def find_part(email, types):
  29.     if not email.is_multipart():
  30.         return email
  31.     
  32.     results = (dict,)((lambda .0: for part in .0:
  33. if part.get_content_type() in types:
  34. (part.get_content_type(), part)continue)(email))
  35.     print results
  36.     for ty in types:
  37.         if ty in results:
  38.             return results[ty]
  39.             continue
  40.     
  41.  
  42.  
  43. def find_attachments(email):
  44.     attachments = { }
  45.     for part in email:
  46.         if 'Content-Disposition' in part.keys() and 'attachment' in part['Content-Disposition']:
  47.             attachments[part.get_filename()] = Storage(data = part.get_payload(decode = True), content_type = part.get_content_type())
  48.             continue
  49.     
  50.     return attachments
  51.  
  52.  
  53. def parse_content(part):
  54.     charset = part.get_content_charset()
  55.     content_type = part.get_content_type()
  56.     payload = part.get_payload(decode = True)
  57.     html = content_type == 'text/html'
  58.     if payload is None:
  59.         payload = ''
  60.     
  61.     
  62.     try:
  63.         if not charset:
  64.             pass
  65.         content = payload.decode('ascii')
  66.     except (UnicodeDecodeError, LookupError):
  67.         content = payload.decode('utf-8', 'replace')
  68.  
  69.     if html:
  70.         content = strip_html_and_tags(content, [
  71.             'style'])
  72.     else:
  73.         content = content
  74.     return content
  75.  
  76.  
  77. class Email(object):
  78.     
  79.     def __init__(self, id = None, fromname = None, fromemail = None, sendtime = None, subject = None, content = None, attachments = None, labels = None):
  80.         autoassign(self, locals())
  81.  
  82.     
  83.     def update(self, email):
  84.         if isinstance(email, dict):
  85.             attrs = email
  86.         else:
  87.             attrs = vars(email)
  88.         autoassign(self, dict((lambda .0: for k, v in .0:
  89. if v is not None:
  90. (k, v)continue)(attrs.iteritems())))
  91.  
  92.     
  93.     def fromEmailMessage(cls, id, email, sendtime_if_error = None):
  94.         encoding = email.get_content_charset()
  95.         (realname, email_address) = parseaddr(email['From'])
  96.         realname = unicode_hdr(realname, encoding)
  97.         _email = email
  98.         
  99.         try:
  100.             datetuple = parsedate(email['Date'])
  101.             sendtime = datetime(*datetuple[:7])
  102.         except Exception:
  103.             traceback.print_exc()
  104.             print >>sys.stderr, 'using %s for "sendtime" instead' % sendtime_if_error
  105.             sendtime = sendtime_if_error
  106.  
  107.         
  108.         try:
  109.             attachments = find_attachments(email)
  110.         except:
  111.             attachments = { }
  112.  
  113.         part = find_part(email, ('text/plain', 'text/html'))
  114.         if part is None:
  115.             content = u''
  116.         else:
  117.             content = parse_content(part)
  118.         content = replace_newlines(content)
  119.         prev_length = pref('email.preview_length', 200)
  120.         if len(content) > prev_length:
  121.             content = content[:prev_length] + '...'
  122.         else:
  123.             content
  124.         email = cls(id, realname, email_address, sendtime, email['Subject'], content = content, attachments = attachments)
  125.         return email
  126.  
  127.     fromEmailMessage = classmethod(fromEmailMessage)
  128.     
  129.     def __eq__(self, other):
  130.         if not isinstance(other, Email):
  131.             return False
  132.         
  133.         return self.id == other.id
  134.  
  135.     
  136.     def __cmp__(self, other):
  137.         
  138.         try:
  139.             return -cmp(self.sendtime, other.sendtime)
  140.         except TypeError:
  141.             return -1
  142.  
  143.  
  144.     
  145.     def domain(self):
  146.         f = self.fromemail
  147.         if f is not None:
  148.             return f[f.find('@') + 1:]
  149.         
  150.  
  151.     domain = property(domain)
  152.     
  153.     def __unicode__(self):
  154.         if not self.subject:
  155.             pass
  156.         lines = [
  157.             unicode('Subject: %s' % '<none>').encode('utf-8')]
  158.         if self.fromname:
  159.             _fromstr = self.fromname
  160.             if self.fromemail:
  161.                 _fromstr += ' <%s>' % self.fromemail
  162.             
  163.         elif self.fromemail:
  164.             _fromstr = self.fromemail
  165.         else:
  166.             _fromstr = '<unknown>'
  167.         lines.append('From: %s' % _fromstr)
  168.         if self.sendtime:
  169.             lines.append('Sent at %s' % self.sendtime)
  170.         
  171.         if self.content:
  172.             lines.append('')
  173.             lines.append(unicode(self.content))
  174.         
  175.         self.lines = lines
  176.         return u''.join(lines)
  177.  
  178.     
  179.     __str__ = lambda self: self.__unicode__().decode('ascii', 'ignore')
  180.     
  181.     def __repr__(self):
  182.         
  183.         try:
  184.             if not self.fromemail:
  185.                 pass
  186.             return u'<Email (Subject: %r, From: %r)>' % (self.subject[:30], self.fromname)
  187.         except Exception:
  188.             return u'<Email from %r>' % self.fromemail
  189.  
  190.  
  191.  
  192.  
  193. class DecodedEmail(email.message.Message):
  194.     
  195.     def __init__(self, myemail):
  196.         self.email = myemail
  197.  
  198.     
  199.     def __getattr__(self, attr, val = sentinel):
  200.         result = getattr(self.email, attr, val)
  201.         if result is sentinel:
  202.             raise AttributeError
  203.         else:
  204.             return result
  205.  
  206.     
  207.     def __getitem__(self, header):
  208.         s = email.message.Message.__getitem__(self, header)
  209.         if header == 'Content':
  210.             return s
  211.         else:
  212.             return unicode_hdr(s, self.get_content_charset())
  213.  
  214.     __iter__ = email.message.Message.walk
  215.  
  216.